package camel.server.session;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

import camel.client.SessionTicket;
import camel.server.admin.TeamMember;

@Entity
@NamedQueries ( {
	@NamedQuery(name = "findLoginToken", query = "SELECT t FROM LoginSessionToken t WHERE t.sn = :sn AND t.token = :token")
})
public class LoginSessionToken {
	private Long sn;
	private String token;
	private Date timeCreated = new Date();

    private Long lastHeartbeat = timeCreated.getTime();
	
    private TeamMember member;
	private ParticipantCursor cursor;
	private CollaborationSession session;
	
	private TeamMember.MemberRole role = TeamMember.MemberRole.disabled;
		
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	public Long getSn() {
		return sn;
	}

	public void setSn(Long sn) {
		this.sn = sn;
	}

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	public Date getTimeCreated() {
		return timeCreated;
	}

	public void setTimeCreated(Date timeCreated) {
		this.timeCreated = timeCreated;
	}

	public void setCursor(ParticipantCursor cursor) {
		this.cursor = cursor;
	}

	@OneToOne
	public ParticipantCursor getCursor() {
		return cursor;
	}

	public void setSession(CollaborationSession session) {
		this.session = session;
	}

	@ManyToOne
	public CollaborationSession getSession() {
		return session;
	}
	
    public Long getLastHeartbeat() {
        return lastHeartbeat;
    }

    public void setLastHeartbeat(Long lastHeartbeat) {
        this.lastHeartbeat = lastHeartbeat;
    }
    
    public void touch () {
    	this.lastHeartbeat = System.currentTimeMillis();
    }

	public void setToken(String token) {
		this.token = token;
	}
	
	public void generateToken () {
		this.token = "CAMEL_SERVER_" + this.sn;
	}
	
	public void populateTicket (SessionTicket ticket) {
		ticket.setLastOpSuccessful(false);
		ticket.setInSession(false);
		ticket.setSessionId(new Long(-1));
		ticket.setCursorId(new Long(-1));
		ticket.setUsername(null);
		ticket.setLogInSuccessful(false);
		ticket.setSn(this.getSn());
		ticket.setToken(this.getToken());
		
		if(getMember() != null) {
			if(getMember().getLoginState() == TeamMember.LoginState.online) {
				ticket.setLogInSuccessful(true);
				ticket.setUsername(getMember().getId());
				ticket.setRole(getMember().getMemberRole());
			}
		}
		
    	if(( getSession() != null ) && ( getCursor() != null )) {
    		if(getCursor().isActiveInSession()) {
    			ticket.setInSession( true );
    			ticket.setSessionId(getSession().getId());
    			ticket.setCursorId(getCursor().getId());
    		}
    	}
	}

	public String getToken() {
		return token;
	}

	@ManyToOne
	public TeamMember getMember() {
		return member;
	}

	public void setMember(TeamMember member) {
		this.member = member;
	}

	public void setRole(TeamMember.MemberRole role) {
		this.role = role;
	}

	public TeamMember.MemberRole getRole() {
		return role;
	}

}
